package remote.service;

import client.common.result.Result;
import client.common.vo.PermissionVO;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import remote.common.constant.UrlConstant;
import remote.common.storage.TokenStorage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 权限服务接口
 */
public interface PermissionService {
    /**
     * 获取权限列表
     *
     * @return 所有的权限列表
     */
    default Result<List<PermissionVO>> getPermissionList() {
        String resultStr = HttpRequest.get(UrlConstant.BASE_URL + "/permission/getPermissions")
                .header("Authorization", TokenStorage.getToken())
                .execute().body();
        System.out.println(resultStr);
        return JSON.parseObject(resultStr, new TypeReference<Result<List<PermissionVO>>>() {
        });
    }
    
    /**
     * 通过角色id获取角色的权限列表
     */
    default Result<List<PermissionVO>> getPermissionListByRole(Integer roleId) {
        Map<String, Object> requestParams = new HashMap<>();
        requestParams.put("roleId", roleId);
        String resultStr = HttpRequest.get(UrlConstant.BASE_URL + "/permission/getPermissionByRoleId")
                .form(requestParams)
                .header("Authorization", TokenStorage.getToken())
                .execute().body();
        System.out.println(resultStr);
        return JSON.parseObject(resultStr, new TypeReference<Result<List<PermissionVO>>>() {
        });
    }
    
    /**
     * 通过角色id修改角色的权限列表
     */
    default Result<List<PermissionVO>> alterPermissionFormRole(Integer roleId,
                                                               String addPermissionStr,
                                                               String delPermissionStr) {
        Map<String, Object> requestParams = new HashMap<>();
        requestParams.put("roleId", roleId);
        requestParams.put("addPermissionStr", addPermissionStr);
        requestParams.put("delPermissionStr", delPermissionStr);
        String resultStr = HttpRequest.put(UrlConstant.BASE_URL + "/permission/alterPermissionFormRole")
                .form(requestParams)
                .header("Authorization", TokenStorage.getToken())
                .execute().body();
        System.out.println(resultStr);
        return JSON.parseObject(resultStr, new TypeReference<Result<List<PermissionVO>>>() {
        });
    }
}
